﻿//
//
/////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using DataAccess;
using PhysicalEquation.Models;

/////////////////////////////////////////////////////////////////////////////
namespace PhysicalEquation.Controllers
{
    /////////////////////////////////////////////////////////////////////////
    [Authorize]
    public class SubjectController : Controller
    {
        //
        // GET: /Subject/
        /////////////////////////////////////////////////////////////////////
        public ActionResult Index()
        {
            List<SubjectModel> models = new List<SubjectModel>();
            try
            {
                List<Subject> subjects = DbAccess.Instance.Entities.Subjects.ToList();
                if (subjects.Count > 0)
                {
                    models = subjects.Select(subject => new SubjectModel()
                    {
                        Id = subject.Id.ToString(CultureInfo.InvariantCulture),
                        Name = subject.Name,
                        Description = subject.Description,
                        UserId = (subject.User_Id != null ? subject.User_Id.ToString() : "0"),
                        UserName = subject.User.Name
                    }).ToList();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return View("Index", models);
        }

        /////////////////////////////////////////////////////////////////////
        public PartialViewResult AddSubject(string name, string description)
        {
            if (ModelState.IsValid)
            {
                Subject newSubject = new Subject()
                {
                    Name = name,
                    Description = description
                };
                newSubject.UserReference.Value = (User)Session["UserInfo"];

                DbAccess.Instance.Entities.Subjects.AddObject(newSubject);
                DbAccess.Instance.Entities.SaveChanges();

                ModelState.Clear();
                return PartialView("AddSubjectRowView", new SubjectModel()
                {
                    Id = newSubject.Id.ToString(CultureInfo.InvariantCulture),
                    Name = newSubject.Name,
                    UserName = newSubject.User.Name,
                    Description = newSubject.Description
                });
            }
            return null;
        }

        //
        // GET: /Subject/Details/5
        /////////////////////////////////////////////////////////////////////
        public ActionResult Details(int id)
        {
            return null;
        }

        //
        // GET: /Subject/Create

        public ActionResult Create()
        {
            return null;
        }

        //
        // POST: /Subject/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return null;
            }
        }

        //
        // GET: /Subject/Edit/5

        public ActionResult Edit(int id)
        {
            return null;
        }

        //
        // POST: /Subject/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return null;
            }
        }

        //
        // GET: /Subject/Delete/5

        public ActionResult Delete(int id)
        {
            return null;
        }

        //
        // POST: /Subject/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return null;
            }
        }
    }
/////////////////////////////////////////////////////////////////////////////
}//PhysicalEquation.Controllers